package com.todod.base.module.base.dal.mysql.device;

import com.todod.base.framework.common.pojo.PageResult;
import com.todod.base.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.todod.base.framework.mybatis.core.mapper.BaseMapperX;
import com.todod.base.module.base.dal.dataobject.device.DeviceDO;
import org.apache.ibatis.annotations.Mapper;
import com.todod.base.module.base.controller.admin.device.vo.*;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

/**
 * 仪器设备 Mapper
 *
 * @author 平台
 */
@Mapper
public interface DeviceMapper extends BaseMapperX<DeviceDO> {

    default PageResult<DeviceDO> selectPage(DevicePageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<DeviceDO>()
                .eqIfPresent(DeviceDO::getDeviceSiteCode, reqVO.getDeviceSiteCode())
                .eqIfPresent(DeviceDO::getDeviceModeCode, reqVO.getDeviceModeCode())
                .eqIfPresent(DeviceDO::getBdType, reqVO.getBdType())
                .likeIfPresent(DeviceDO::getBdMode, reqVO.getBdMode())
                .eqIfPresent(DeviceDO::getFactoryCode, reqVO.getFactoryCode())
                .eqIfPresent(DeviceDO::getState, reqVO.getState())
                .betweenIfPresent(DeviceDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(DeviceDO::getCreateTime)
        );
    }

    //验证仪器同类型重复数量
    @Select("SELECT COUNT(*) FROM base_device WHERE  factory_code  = #{factoryCode} AND bd_type = #{bdType} AND bd_mode = #{bdMode} AND deleted = 0")
    public int validateDeviceExist(DeviceSaveReqVO createReqVO);



    //获取仪器类型和厂家的相同的最大数量
    @Select("SELECT COUNT(*) from base_device WHERE  factory_code  = #{factoryCode} AND bd_type = #{bdType} AND deleted = 0")
    public int getDeviceMax(DeviceSaveReqVO createReqVO);



    /**
     * 启用仪器设备
     *
     * @param id 编号
     */
    @Update("update base_device set state = 1   where id = #{id} AND deleted = 0")
    public void enableDevice(Long id);

    /**
     * 禁用仪器设备
     *
     * @param id 编号
     */
    @Update("update base_device set state = 0   where id = #{id} AND deleted = 0")
    public void disableDevice(Long id);
}